const util = require('../../../utils/util.js')
const app = getApp()
const formatNumber = util.formatNumber
const getRandom = util.getRandom
Page({
  data: {
    cinemaId: '',
    movieId: '',
    cinemaDetail: null, //影院详情
    movie: null, //选中的电影
    movies:null, //电影列表
    days: [], //该电影的排片日期列表
    timeList: [], //当天播放电影的时间段
    divideDealList: [], //影院分类零食列表
    first:true, //只在第一次提示
    map: {}
  }, 
  onLoad(query) {
   this.initPage(query)
  },
  //初始化页面
  initPage(query) {
    const { cinemaId = '', movieId = '' } = query
    this.setData({
      cinemaId,
      movieId,
      cinemaDetail: {
        latitude: query.latitude, // 纬度
        longitude: query.longitude, // 经度
        addr: query.cinemaAddr, // 地址
        nm: query.cinemaName //店名
      },
    })
    this.getCinemaShows(movieId)
  },
  // 根据电影和影院获取影片上映场次
  getCinemaShows(movieId){
    const _this = this
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      url: app.globalData.url + `/movie/getCinemaShows`,
      method: 'GET',
      data: {
        cinemaId: _this.data.cinemaId,
        movieId
      },
      success(res) {
        wx.hideLoading()
        const {films,map} = res.data.data
        const day =  Object.keys(res.data.data.map) && Object.keys(res.data.data.map)[0];
        const movie = films.filter(item=> item.movieId === movieId)[0];
        _this.setData({
          movies: films,  // 
          movie,
          days: _this.computedTime(Object.keys(res.data.data.map), day),
          day,
          map,
          timeList: map[day]
        })
      }
    })
  },
  getall(movieId){
    const _this = this
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      url: app.globalData.url + `/movie/getCinemaShows`,
      method: 'GET',
      data: {
        cinemaId: _this.data.cinemaId,
        movieId
      },
      success(res) {
        wx.hideLoading()
        const {films,map} = res.data.data
        const day =  Object.keys(res.data.data.map) && Object.keys(res.data.data.map)[0];
        const movie = films.filter(item=> item.movieId === movieId)[0];
        _this.setData({
          movie,
          days: _this.computedTime(Object.keys(res.data.data.map), day),
          day,
          map,
          timeList: map[day],
        })
      }
    })
  },
  //选择电影
  selectMovie(e) {
    const movie = e.detail.movie
    this.setData({
      movie,
    })
    this.getall(movie.movieId)
  },
  //选择时间
  selectDay(e) {
    const day = e.detail.day;
    this.setData({
      timeList: this.data.map[`${day}`]
    })
  },
  //跳转到“套餐详情”页面
  goSnackPage(e){
    const info = e.currentTarget.dataset.info;
    console.log(e.currentTarget);
    console.log(info);
    //将参数转化为JSON通过页面跳转时传递
    const paramsStr = JSON.stringify({
      cinemaName: this.data.cinemaDetail.nm,
      cinemaId: this.data.cinemaId,
      // showId:
      cinemaData: this.data.cinemaDetail,//影院信息
      info: info
    })
    wx.navigateTo({
      url: `/pages/subPages/snack-page/snack-page?paramsStr=${encodeURIComponent(paramsStr)}`,
    })
  },
  //购票
  buyTicket(e){
    const info = JSON.stringify(e.currentTarget.dataset.info);
    const movie = JSON.stringify(this.data.movie);
    const cinema = JSON.stringify(this.data.cinemaDetail);
    wx.navigateTo({
      url: `/pages/subPages/seat/hall?movie=${movie}&info=${info}&cinema=${cinema}`,
    })
  },
  // 转换时间格式
  computedTime(data, start){
    const todayTomorrow = ['今天', '明天', '后天']
    const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    let days = []
    for (let i = 0; i < data.length; i++) {
      let day = new Date(start)
      day.setDate(day.getDate() + i) //往后推几天
      const num = (day - new Date(util.getToday())) / (1000 * 60 * 60 * 24) //计算相隔几天，减少必须是“今天”0时0分
      days.push({
        title: `${todayTomorrow[num] || week[day.getDay()]}${day.getMonth() + 1}月${day.getDate()}日`, //获取类似 “后天9月1日” 的字符串
        day: data[i]
      })
    }
    return days
  }
})